package com.espirit.index;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;

import org.apache.struts2.ServletActionContext;
import org.json.JSONArray;
import org.json.JSONObject;

import com.espirit.eap.BaseAction;
import com.espirit.eap.util.Functions;
import com.googlecode.cswish.security.SafeManager;
import com.googlecode.cswish.security.auth.UserInfo;
import com.googlecode.cswish.struts.ContextHolder;

public class ExecuteAction extends BaseAction {

	private List<String> mainPanelLabels;
	
	private List<String> mainPanelLinks;
	
	private int selected;
	
	@Resource
	private SafeManager safeManager;
	
	@Resource
	private Functions functions;
	
	public List<String> getMainPanelLabels() {
		return mainPanelLabels;
	}

	public void setMainPanelLabels(List<String> mainPanelLabels) {
		this.mainPanelLabels = mainPanelLabels;
	}

	public List<String> getMainPanelLinks() {
		return mainPanelLinks;
	}

	public void setMainPanelLinks(List<String> mainPanelLinks) {
		this.mainPanelLinks = mainPanelLinks;
	}
	
	public int getSelected() {
		return selected;
	}

	public void setSelected(int selected) {
		this.selected = selected;
	}

	public String execute() {
		List<String> mainTabTitles = new ArrayList<String>();
		this.mainPanelLabels = mainTabTitles;
		
		List<String> mainTabUrls = new ArrayList<String>();
		this.mainPanelLinks = mainTabUrls;
		this.selected = 0;
		
		UserInfo userInfo = (UserInfo)safeManager.getLoginUser(ServletActionContext.getRequest());
		String loginId = userInfo.getLoginId();
		
		String notBlankMessagePage = "/espirit/common/message.search.html?newSearch=true";
		String blankMessagePage = "/espirit/common/message.blank.html";
		String messagePage;
		boolean hasMessagePagePermission = safeManager.checkPage(loginId,
					functions.linkNoBase("/espirit/common/message.search.html", true));
		if (hasMessagePagePermission) {
			messagePage = notBlankMessagePage;
		} else {
			messagePage = blankMessagePage;
		}
		/*
		try {
			String base = Functions.getBase();
			Cookie[] cookies = ServletActionContext.getRequest().getCookies();
			Map<String, Boolean> duplicatedUrl = new HashMap<String, Boolean>();
			for (Cookie cookie : cookies) {
				if ("openedTabs".equals(cookie.getName())) {
					String val = cookie.getValue();
					String decodeVal = URLDecoder.decode(val, "UTF-8");
					JSONObject openedTabs = new JSONObject(decodeVal);
					this.selected = openedTabs.getInt("selected");
					JSONArray jsonArray = openedTabs.getJSONArray("tabs");
					int len = jsonArray.length();
					for (int i = 0; i < len; i++) {
						JSONObject jsonObject = jsonArray.getJSONObject(i);
						String target = jsonObject.getString("target");
						String url = jsonObject.getString("url");
						if (blankMessagePage.equals(url) || notBlankMessagePage.equals(url)) {
							url = messagePage;
						}
						
						if (!duplicatedUrl.containsKey(target)) {
							int index = url.lastIndexOf('?');
							String checkPath;
							if (index > 0) {
								checkPath = functions.linkNoBase(url.substring(0, index), true);
							} else {
								checkPath = functions.linkNoBase(url, true);
							}
							if (safeManager.checkPage(loginId, checkPath)) {
								mainPanelLabels.add(target);
								
								mainPanelLinks.add(url);
								duplicatedUrl.put(target, Boolean.TRUE);
							}
						}
					}
					break;
				}
			}
		} catch (Exception ex) {
			logger.error("Wrong cookie data", ex);
		}
		
		
		boolean hasMessagePage = false;
		for (String url : mainTabUrls) {
			if (url.indexOf(messagePage) >= 0) {
				hasMessagePage = true;
				break;
			}
		}
		
		
		if (!hasMessagePage) {
			mainTabTitles.add(0, "commons.main");
			mainTabUrls.add(0, messagePage);
		}
		*/
		
		mainTabTitles.add(0, "commons.main");
		mainTabUrls.add(0, messagePage);
		return SUCCESS;
	}
}
